package com.charging.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.charging.common.constant.Constants;
import com.charging.common.utils.DateUtils;
import com.charging.system.domain.KxRechargeRecord;
import com.charging.system.service.IKxRechargeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.charging.system.mapper.KxCardMapper;
import com.charging.system.domain.KxCard;
import com.charging.system.service.IKxCardService;

import javax.annotation.Resource;

/**
 * 电卡管理Service业务层处理
 *
 * @author charging
 * @date 2024-04-19
 */
@Service
public class KxCardServiceImpl implements IKxCardService
{
    @Resource
    private KxCardMapper kxCardMapper;
    @Resource
    private IKxRechargeRecordService kxRechargeRecordService;

    /**
     * 查询电卡管理
     *
     * @param cardId 电卡管理主键
     * @return 电卡管理
     */
    @Override
    public KxCard selectKxCardByCardId(Long cardId)
    {
        return kxCardMapper.selectKxCardByCardId(cardId);
    }

    /**
     * 查询电卡管理列表
     *
     * @param kxCard 电卡管理
     * @return 电卡管理
     */
    @Override
    public List<KxCard> selectKxCardList(KxCard kxCard)
    {
        return kxCardMapper.selectKxCardList(kxCard);
    }

    /**
     * 新增电卡管理
     *
     * @param kxCard 电卡管理
     * @return 结果
     */
    @Override
    public int insertKxCard(KxCard kxCard)
    {
        kxCard.setCreateTime(DateUtils.getNowDate());
        return kxCardMapper.insertKxCard(kxCard);
    }

    /**
     * 修改电卡管理
     *
     * @param kxCard 电卡管理
     * @return 结果
     */
    @Override
    public int updateKxCard(KxCard kxCard)
    {
        kxCard.setUpdateTime(DateUtils.getNowDate());
        return kxCardMapper.updateKxCard(kxCard);
    }

    /**
     * 批量删除电卡管理
     *
     * @param cardIds 需要删除的电卡管理主键
     * @return 结果
     */
    @Override
    public int deleteKxCardByCardIds(Long[] cardIds)
    {
        return kxCardMapper.deleteKxCardByCardIds(cardIds);
    }

    /**
     * 删除电卡管理信息
     *
     * @param cardId 电卡管理主键
     * @return 结果
     */
    @Override
    public int deleteKxCardByCardId(Long cardId)
    {
        return kxCardMapper.deleteKxCardByCardId(cardId);
    }

    /**
     * 通过电卡号查询数据
     * @param cardNo
     * @return
     */
    @Override
    public KxCard selectKxCardByCardNo(String cardNo) {
        return kxCardMapper.selectKxCardByCardNo(cardNo);
    }

    /**
     * 更新电卡余额
     * @param kxCard
     * @return
     */
    @Override
    public int updateBalance(KxCard kxCard) {
        return kxCardMapper.updateBalance(kxCard);
    }

    /**
     * 电卡赠送金额
     * @param cardId
     * @param give
     * @return
     */
    @Override
    public int giveCard(Long cardId, BigDecimal give) {
        KxCard kxCard = kxCardMapper.selectKxCardByCardId(cardId);
        KxRechargeRecord kxRechargeRecord = new KxRechargeRecord();
        kxRechargeRecord.setCardId(cardId);
        kxRechargeRecord.setCardNo(kxCard.getCardNo());
        kxRechargeRecord.setTranType(Constants.TranType.NOKNOW);
        kxRechargeRecord.setTranStatus(Constants.TranStatus.YES);
        kxRechargeRecord.setRechargeSchemeId(100L);
        kxRechargeRecord.setBalance(BigDecimal.ZERO);
        kxRechargeRecord.setGive(give);
        kxRechargeRecordService.insertKxRechargeRecord(kxRechargeRecord);
        KxCard newKxCard = new KxCard();
        newKxCard.setCardId(cardId);
        newKxCard.setGive(kxCard.getGive().add(give));
        return kxCardMapper.updateKxCard(newKxCard);
    }
}
